package com.foreknow.controller;

import com.foreknow.bean.Guestbook;
import com.foreknow.service.AdminService;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.AdminServiceImpl;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Add extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //        设置服务器向客户端响应类型
        resp.setContentType("text/html;charset=utf-8");
//        设置客户端反馈服务器为utf8
        req.setCharacterEncoding("utf-8");

        Date date = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");


        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        String createdtime = req.getParameter("createdtime");

        //        调用Model servers 和dao   连接数据库
       GuestbookService service = new GuestbookServiceImpl();
//       创建guest对象 吧表单的数据录入对象
       Guestbook guestbook = new Guestbook();
       guestbook.setId(Integer.parseInt(id));
       guestbook.setName(name);
       guestbook.setPhone(phone);
       guestbook.setEmail(email);
       guestbook.setTitle(title);
       guestbook.setContent(content);
       guestbook.setCreatedtime(simpleDateFormat.format(date));

//       吧对象加入数据库
        Guestbook add = service.add(guestbook);
//跳转显示界面
        ManageServlet manageServlet = new ManageServlet();
        manageServlet.list(req,resp);





    }
}
